package modulesAPI.v2.composite;

import java.util.GregorianCalendar;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;
import person.Employee;
import person.Employee.Role;
import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModDoubleTextField;
import modulesAPI.v2.basics.ModTextField;

public class MEmployee extends Module<Employee>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un dipendente
	 * */
	public MEmployee(Module<?> ownerModule, Employee content, String tag, String description, ViewType viewType,int... options) {
		super(ownerModule, content, tag, description, viewType,options);
		this.createModules();
		this.addModules(person, birth, refC, role, salary);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un dipendente
	 * */
	public MEmployee(Module<?> ownerModule, String tag, String description, ViewType viewType) {
		super(ownerModule, new Employee(Model.NO_ID, "", "", "", "", "", "", "", Role.OTHER_EMPLOYEE, "", new GregorianCalendar(), 0), tag, description, viewType);
		this.createModules();
		this.addModules(person, birth, refC, role, salary);
	}
	
	private void createModules(){
		person = new MPerson(this,this.getContent(),"EMPLOYEE_DETAILS", "EMPLOYEE_DETAILS_DESC", this.getViewType());
		role = new ModComboBox(this, this.getContent().getRole(), "EMPLOYEE_ROLE", "EMPLOYEE_ROLE_DESC", this.getViewType(), Employee.Role.values());
		refC = new ModTextField(this, this.getContent().getReferenceCompany(), "EMPLOYEE_REFERENCE_COMPANY", "EMPLOYEE_REFERENCE_COMPANY_DESC", this.getViewType());
		birth = new ModCalendar(this, this.getContent().getBirthDate(), "EMPLOYEE_BIRTH_DATE", "EMPLOYEE_BIRTH_DATE_DESC", this.getViewType());
		salary = new ModDoubleTextField(this, this.getContent().getSalary(), "EMPLOYEE_SALARY", "EMPLOYEE_SALARY_DESC", this.getViewType());
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
			return CModules.formatForList(this.getContent().getName()+" "+this.getContent().getSurname(), 
					CWords.getWord(this.getContent().getRole().name()),isSelected);
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		Employee e = new Employee(
				this.getContent().getId(),
				person.getContent(),
				(Role)role.getContent(),
				refC.getContent(),
				birth.getContent(),
				salary.getContent()
				);
		this.setContent(e);
	}
	@Override
	protected void afterFireChange(Module<?> mod) {	
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	private MPerson person;
	private ModComboBox role;
	private ModTextField refC;
	private ModDoubleTextField salary;
	private ModCalendar birth;
}
